package com.qc.bus.controller;


import com.baomidou.mybatisplus.extension.api.ApiController;
import com.qc.bus.entity.BusGoods;
import com.qc.bus.service.BusGoodsService;
import com.qc.bus.vo.GoodsVo;
import com.qc.sys.common.Constant;
import com.qc.sys.common.DataGridView;
import com.qc.sys.common.ResultObj;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * (BusGoods)表控制层
 *
 * @author AltmanQccc
 * @since 2020-09-09 14:28:31
 */
@RestController
@RequestMapping("goods")
public class BusGoodsController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private BusGoodsService busGoodsService;

    /**
     * 查询并展示数据
     * */
    @GetMapping("loadAllGoods")
    public Object loadAllGoods(GoodsVo goodsVo){
        return this.busGoodsService.queryAllGoods(goodsVo);
    }

    /**
     * 添加客户
     * */
    @PostMapping("addGoods")
    public ResultObj addGoods(BusGoods goods){
        try {
            goods.setAvailable(Constant.USER_AVAILABLE_TRUE);
            this.busGoodsService.InsertGoods(goods);
            return ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 修改客户
     * */
    @PostMapping("updateGoods")
    public ResultObj updateGoods(BusGoods goods){
        try {
            this.busGoodsService.UpdateGoods(goods);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    /**
     * 删除客户
     * */
    @PostMapping("deleteGoods")
    public ResultObj deleteGoods(Integer id){
        try {
            this.busGoodsService.removeById(id);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    /**
     * 批量删除
     */
    @PostMapping("batchDeleteGoods")
    public ResultObj batchDeleteGoods(Integer[] ids) {
    //1.对传过来的ids进行判断

    //2.进行遍历删除
    //3.不符合类型的返回错误
        try {
            if (null!=ids&&ids.length>0){
                List<Integer> idList = new ArrayList<>();
                for (Integer id : ids) {
                    idList.add(id);
                }
                this.busGoodsService.removeByIds(idList);
                return ResultObj.DELETE_SUCCESS;
            }else {
                return new ResultObj(-1,"传入ids异常，删除错误！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    /**
     * 查询可用的供应商
     * */
    @GetMapping("getAllAvailableGoods")
    public Object getAllAvailableGoods(){
        return this.busGoodsService.queryAllAvailableGoods();
    }

    /**
     * 通过供应商id查询底下的商品
     * */
    @GetMapping("getGoodsByProviderId")
    public Object getGoodsByProviderId(Integer providerid){
       return this.busGoodsService.queryGoodsByProviderId(providerid);
    }

    /**
     * 根据商品ID查询商品信息
     */
    @GetMapping("getGoodsByGoodId")
    public Object getGoodsByGoodId(Integer goodsid){
        return new DataGridView(this.busGoodsService.getById(goodsid));
    }
}